package whu.o2o;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import whu.o2o.View.ExpandGridView;
import whu.o2o.util.ConstantData;
import whu.o2o.util.ImeUtil;
import whu.o2o.util.Utils;
import whu.o2o.vo.GuideVO;
import whu.o2o.vo.PersonVO;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import whu.o2o.R;
import whu.o2o.R.drawable;


public class MainActivity extends CommonActivity implements OnClickListener,OnItemClickListener{
	private ExpandGridView mFunctionGridView;
	private Context mcontext;
	private LinearLayout lin_morecall,lin_nouser;
	private RelativeLayout rel_bottom,rebottom_head;
	private ImageButton ib_register;
	private ImageButton ib_login;
	private TextView tv_name,tv_age,tv_personal;
	private ImageView iv_head;
	private SharedPreferences sp;
//	private Class<?>[] mActivitys = {GuideDiaryAcivity.class,GuideActivity.class,GuideActivity.class,GuideActivity.class, 
//			GuideActivity.class,GuideActivity.class,  GuideActivity.class, GuideActivity.class};
	String AUDIO_PATH;
	String fileName;
	private ReceiverHandler rh;
	private Boolean flag = false;
	private LocationClient mLocationClient;
	private MyLocationListenner mMyLocationListenner;
	private PersonVO mperson;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		initView();
		initFunctionData();
		initLocation();
	}
	
	private void initView() {
		mcontext = this;
		rh=new ReceiverHandler(this);
        rh.registerAction(ConstantData.Action.loginTag);
		ib_login = (ImageButton) findViewById(R.id.bt_main_login);
		ib_register = (ImageButton) findViewById(R.id.bt_main_register);
		mFunctionGridView = (ExpandGridView) findViewById(R.id.gv_main_menu);
		lin_morecall = (LinearLayout) findViewById(R.id.lin_main_morecall);
		
		tv_name = (TextView) findViewById(R.id.tv_main_name);
		tv_age = (TextView) findViewById(R.id.tv_main_age);
		tv_personal = (TextView) findViewById(R.id.tv_main_personal);
		
		rel_bottom = (RelativeLayout) findViewById(R.id.re_main_bottom);
		rebottom_head = (RelativeLayout) findViewById(R.id.re_bottom_head);
		lin_nouser = (LinearLayout) findViewById(R.id.lin_no_user);
		sp = getSharedPreferences("info", MODE_PRIVATE);
		
		ib_register.setOnClickListener(this);
		ib_login.setOnClickListener(this);
		lin_morecall.setOnClickListener(this);
		tv_personal.setOnClickListener(this);
		
		
		mLocationClient = new LocationClient(mContext);
		mMyLocationListenner = new MyLocationListenner();
         if(mApplication.getUserId()!=0){
        	 changeState();
         }
	}
	
	/**
	 * 
	 */
	private void initFunctionData() {
		String[] texts = getResources().getStringArray(R.array.main_function);
		int[] icos = { R.drawable.ib_main_gr1_sel, R.drawable.ib_main_gr2_sel, R.drawable.ib_main_gr7_sel, R.drawable.ib_main_gr6_sel, R.drawable.ib_main_gr3_sel,
				R.drawable.ib_main_gr4_sel, R.drawable.ib_main_gr8_sel, R.drawable.ib_main_gr5_sel };

		ArrayList<HashMap<String, Object>> imagelist = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < 8; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("ico", icos[i]);
			map.put("text", texts[i]);
			imagelist.add(map);
		}

		SimpleAdapter simpleAdapter = new SimpleAdapter(mcontext, imagelist, R.layout.griditem_function, new String[] { "ico", "text" }, new int[] { R.id.ico, R.id.text });
		mFunctionGridView.setAdapter(simpleAdapter);
		mFunctionGridView.setOnItemClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (v.getId()) {
		case R.id.bt_main_register:
			intent = new Intent(mcontext, RegisterActivity.class);
			startActivity(intent);
			break;

	
		case R.id.bt_main_login:
		//		recorderInstance.setRecording(false);
		//			SpeexPlayer splayer = new SpeexPlayer(fileName, this);
			//	splayer.startPlay();
			intent = new Intent(mcontext, LoginActivity.class);
			startActivity(intent);
			break;

		case R.id.tv_main_personal:
		//	PersonVO guidevo = mApplication.getUserVO();			
			intent = new Intent(mcontext, GuideCardActivity.class);
			intent.putExtra("card", mperson);
			startActivity(intent);
			break;
		case R.id.lin_main_morecall:
			//	PersonVO guidevo = mApplication.getUserVO();			
				intent = new Intent(mcontext, GuideActivity.class);
				intent.putExtra("card", mperson);
				startActivity(intent);
				break;

		}
		
	}
	
	private void changeState(){
		lin_nouser.setVisibility(View.GONE);
		rel_bottom.setVisibility(View.VISIBLE);
		rebottom_head.setVisibility(View.VISIBLE);
		mperson = mApplication.getUserVO();
		tv_name.setText(mperson.getName());
		tv_age.setText(String.format(mContext.getResources().getString(R.string.age), mperson.getAge()));
		Drawable d_girl = getResources().getDrawable(R.drawable.girl);
		Drawable d_boy = getResources().getDrawable(R.drawable.boy);
		if(mperson.getSex()==0){
			d_boy.setBounds(0, 0, d_boy.getMinimumWidth(), d_boy.getMinimumHeight());
			tv_age.setCompoundDrawables(d_boy, null, null, null);
		}
		else 
		{
			d_girl.setBounds(0, 0, d_girl.getMinimumWidth(), d_girl.getMinimumHeight());
			tv_age.setCompoundDrawables(d_girl, null, null, null);}
		
	
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(this, GuideActivity.class);
		
		startActivity(intent);
		
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(rh);
		mLocationClient.stop();
	}
	
	/**
	 * 接收异步返回的定位结果
	 * 
	 */
	private class MyLocationListenner implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			String city = ConstantData.Data.DEFAULT_CITY;
			Log.d("1111", city);
			if (location != null) {
				if (!TextUtils.isEmpty(location.getCity())) {
					city = Utils.filterCityName(location.getCity());
					
				}
			}

			mApplication.setCity(city);
		

			mLocationClient.stop();
			mLocationClient.unRegisterLocationListener(mMyLocationListenner);

		

			Intent intent = new Intent(ConstantData.Action.LOCATION);
			sendBroadcast(intent);
		}

	}
	
          
	
	/**
	 * 初始化定位设置
	 */
	private void initLocation(){
	

		mLocationClient.registerLocationListener(mMyLocationListenner);

		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(LocationMode.Hight_Accuracy);
		option.setCoorType("bd09ll");
		option.setScanSpan(5000);
		option.setIsNeedAddress(true); // 返回的定位结果包含地址信息
		mLocationClient.setLocOption(option);
		mLocationClient.start();

	}
	
	public class ReceiverHandler extends BroadcastReceiver{

	 	Context context=null;
		public ReceiverHandler(Context context)
		{
			this.context=context;
		}
		 
		public void registerAction(String action){
	      IntentFilter filter=new IntentFilter();
	      filter.addAction(action);
	      context.registerReceiver(this, filter);
		}
		 
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if(intent.getAction().equals(ConstantData.Action.loginTag)){
			//	Log.d("sssss", sp.getString("name", ""));
				changeState();
				
			}
		}
	}


}
